<?php

class Payment_Item_Trial extends Payment_Item_Abstract
{
	const CODE = 13;
	protected $_code = self::CODE;
	private $_is_valid = array();
	private $_has_record = false;
	
	public function getDefaultTime()
	{
		return self::FREE_TRIAL_PERIOD;
	}
	
	public function hasRecord($id_page)
	{
		$this->isValid($id_page);
		return $this->_has_record;
	}
	
	public function isValid($id_page)
	{
		if (isset($this->_is_valid[$id_page]))
		{
			return $this->_is_valid[$id_page];
		}

		$pageItemsModel = Payment_Model_DbTable_PageItems::getInstance();
		$pageItem = $pageItemsModel->findByPageItem($id_page, self::CODE);

		$this->_is_valid[$id_page] = false;

		if (!empty($pageItem))
		{
			$this->_has_record = true;
			if (intval($pageItem->time_expired) - time() > 0)
			{
				$this->_is_valid[$id_page] = true;
			}
		}
		else
		{
			$pageItemsModel->updatePageItem(
					$id_page, $this->_code, time(), $this->getDefaultTime()
			);
			$this->_is_valid[$id_page] = true;
		}

		return $this->_is_valid[$id_page];
	}

}